package com.dq.domain.sys.user.user.base;

import com.dq.vo.baseVo.VO;
import com.dq.domain.sys.menu.SysMenu;
import com.dq.domain.position.OrgPosition;
import com.dq.utils.TimeUtil;
import com.google.common.base.Joiner;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 后台用户
 * @author cf
 */
@Data
@Entity
@ApiModel("后台用户")
public class BackUser extends SysUser implements VO.ToVO {

    /** 非实体字段 */
    @Transient
    protected Long sectionId;
    @Transient
    protected String postIds;
    @Transient
    protected String serviceProjectIds;
    @Transient
    protected String roleIds;
    @Transient
    protected String roleNames;

    @Data
    @ApiModel(value = "用户显示结构")
    public static class Vo implements VO {
        @ApiModelProperty("id")
        protected Long id;
        @ApiModelProperty("真实姓名")
        protected String userRealcName;
        @ApiModelProperty("账号")
        protected String username;
        @ApiModelProperty(value = "头像")
        private String headImg;
        @ApiModelProperty(value = "手机号")
        private String mobile;
        @ApiModelProperty(value = "组织部门/门店")
        private Long sectionId;
        @ApiModelProperty(value = "组织部门/门店")
        private String sectionSimpleName;

        @ApiModelProperty("省份")
        protected String province;
        @ApiModelProperty("城市")
        protected String city;
        @ApiModelProperty("区")
        protected String district;

        @ApiModelProperty("状态")
        protected String statusEn;
        @ApiModelProperty("状态")
        protected String statusCn;
        @ApiModelProperty(value = "创建时间")
        private String createTime;
        @ApiModelProperty(value = "更新时间")
        private String updateTime;

        @ApiModelProperty(value = "角色code列表")
        private Set<String> roleCodes;
        @ApiModelProperty("菜单id列表")
        private List<SysMenu.Vo> menuList;
        @ApiModelProperty("按钮id列表")
        private Set<SysMenu.Vo> buttonList;

        @ApiModelProperty("token")
        private String token;

        protected String postIds;
        protected String roleIds;
        protected String roleNames;
    }

    @Override
    public VO toVO(boolean isRecursion) {
        Vo vo = new Vo();
        vo.id = this.getId();
        vo.userRealcName = fmt(this.getUserRealcName());
        vo.username = fmt(this.getUsername());
        vo.headImg = fmt(this.headImg);
        vo.statusEn = fmt(this.getStatus().toString());
        vo.statusCn = fmt(this.getStatus().name);
        vo.createTime = TimeUtil.longToString(this.createTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.updateTime = TimeUtil.longToString(this.lastModifyTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);

        vo.sectionId = this.getSection() == null ? null : this.getSection().getId();
        vo.sectionSimpleName = this.getSection() == null ? "" : this.getSection().getSimpleName();

        vo.province = fmt(this.getProvince());
        vo.city = fmt(this.getCity());
        vo.district = fmt(this.getDistrict());
        vo.mobile = fmt(this.mobile);
        List<Long> longs = new ArrayList<>();
        OrgPosition orgPosition = this.getOrgPosition();
        Long postId = null;
        if(orgPosition != null){
            postId = orgPosition.getId();
            longs.add(postId);
        }
        vo.postIds = orgPosition!= null? Joiner.on(",").join(longs):"";
        vo.roleIds = this.roleIds;
        vo.roleNames = this.roleNames;
        return vo;
    }
}
